package com.gaoxk.configer;

import com.gaoxk.cxf.service.dao.TServerendpointDao;
import com.gaoxk.cxf.service.s30064000001.ESBServerSoapEndpointImpl;
import com.gaoxk.interceptor.AuthInterceptor;
import com.gaoxk.utils.MapUtil;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
//import org.apache.cxf.interceptor.LoggingInInterceptor;
//import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.ext.logging.LoggingInInterceptor;
import org.apache.cxf.ext.logging.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Configuration
public class WebServiceConfig {

    private static final Logger logger = LoggerFactory.getLogger(ESBServerSoapEndpointImpl.class.getName());
    /*@Resource
    private ESBServerPortType management;*/
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private TServerendpointDao serverendpointdao;
    @Autowired
    private AuthInterceptor authInterceptor;

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus(){
        return new SpringBus();
    }



    /**
     * JAX-WS
     */
//    @Bean
//    @Qualifier("com.gaoxk.cxf.service.s30062000020.ESBServerSoapEndpointImpl")
//    public Endpoint endpoint1(com.gaoxk.cxf.service.s30062000020.ESBServerSoapEndpointImpl esbservice1){
//        //http://localhost:8866/cxf/test?wsdl
//        EndpointImpl endpoint = new EndpointImpl(springBus(), esbservice1);
//        endpoint.publish("/test1");
//        return endpoint;
//    }

//    @Bean(value = "endpoint2")
//    @Qualifier("com.gaoxk.cxf.service.s30064000001.ESBServerSoapEndpointImpl")
//    public Endpoint endpoint2( com.gaoxk.cxf.service.s30064000001.ESBServerSoapEndpointImpl esbservice1){
//        //http://localhost:8866/cxf/test?wsdl
//        EndpointImpl endpoint = new EndpointImpl(springBus(), esbservice1);
//        endpoint.publish("/test2");
//        return endpoint;
//    }

    @Bean
    public String endpoint2( ){
       DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();

        try{
           Object abc =defaultListableBeanFactory.getBean("ESBServerSoapEndpointImpl3");
            EndpointImpl endpoint = new EndpointImpl(springBus(), abc);
            endpoint.publish("s20022000016");
            logger.info("tuichule");
            endpoint.getInInterceptors().add(new LoggingInInterceptor());//记录客户端访问的日志
            endpoint.getOutInterceptors().add(new LoggingOutInterceptor());//记录服务端输出的日志
            defaultListableBeanFactory.registerSingleton("endpoint3", endpoint);
        }catch (Exception e){

        }

        return "";
    }
/*
    @Bean
    public String endpoint( ){
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
        List<Map<String, Object>> serverlist = serverendpointdao.selectAllSxServer();
        Map<String, Object> param = new HashMap<>(0);
        String servbeanname = null;
        String endpbeanname = null;
        String endpurlpath = null;
        String timeout = null;
        String serverid = null;
        if(serverlist.size() >0) {
            try {
                for (Map<String, Object> map : serverlist) {
                    servbeanname = MapUtil.getString(map, "servbeanname");

                    endpbeanname = MapUtil.getString(map, "endpbeanname");
                    endpurlpath = MapUtil.getString(map, "endpurlpath");
                    logger.info("servbeanname=[{}],endpbeanname[{}],endpurlpath[{}]",servbeanname ,endpbeanname,endpurlpath) ;

//                    Object abc = defaultListableBeanFactory.getBean("ESBServerSoapEndpointImpl2");
//                    EndpointImpl endpoint = new EndpointImpl(springBus(), abc);
//                    endpoint.publish("/test2");
//                    logger.info("tuichule");
//                    defaultListableBeanFactory.registerSingleton("endpoint2", endpoint);
                    Object abc = defaultListableBeanFactory.getBean(servbeanname);
                    EndpointImpl endpoint = new EndpointImpl(springBus(), abc);
                    endpoint.publish(endpurlpath);
//                    endpoint.getInInterceptors().add(new ServerInInterceptor());//记录客户端访问的日志
//                    endpoint.getOutInterceptors().add(new ServerOutInterceptor());//记录服务端输出的日志
                    endpoint.getInInterceptors().add(new LoggingInInterceptor());//记录客户端访问的日志
                    endpoint.getInInterceptors().add(authInterceptor);
                    endpoint.getOutInterceptors().add(new LoggingOutInterceptor());//记录服务端输出的日志

                    defaultListableBeanFactory.registerSingleton(endpbeanname, endpoint);
                }
            } catch (Exception e) {
                logger.error("endpoint servers error:",e);
            }

            return "";
        } else {
            logger.info("服务表没有生效的数据");
            return "";
        }
    }
*/

}
